﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmugMugModel;

namespace SmugMugPhotoShow
{
    public partial class LogIn : Form
    {
        private Site mySite = new Site();
        private string UserName = string.Empty;
        private string Password = string.Empty;
        private MyUser myuser = null;
        public MyUser user
        {
            get { return myuser; }
            set { myuser = value; }
        }

        public LogIn()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// Log in to SmugMug using a UserName and Password
        /// </summary>
        private void SmugMugLogIn()
        {
            try
            {
                user = mySite.Login(UserNameTxtBx.Text, PasswordTxtBx.Text);
            }
            catch (SmugMugException sme)
            {
                MessageBox.Show(sme.Message);
                user = null;
            }
        }

        /// <summary>
        /// Defines the actions that are performed when clicking the ClearUserName button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearUserNameBttn_Click(object sender, EventArgs e)
        {
            if (ClearUserNameBttn.Text == "Clear")
            {
                UserName = UserNameTxtBx.Text;
                UserNameTxtBx.Clear();
                ClearUserNameBttn.Text = "Undo";
            }
            else
            {
                if (ClearUserNameBttn.Text == "Undo")
                {
                    UserNameTxtBx.Text = UserName;
                    ClearUserNameBttn.Text = "Clear";
                }
            }
        }

        /// <summary>
        /// Defines the actions that are performed when clicking the ClearPassword button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearPasswordBttn_Click(object sender, EventArgs e)
        {
            if (ClearPasswordBttn.Text == "Clear")
            {
                Password = PasswordTxtBx.Text;
                PasswordTxtBx.Clear();
                ClearPasswordBttn.Text = "Undo";
            }
            else
            {
                if (ClearPasswordBttn.Text == "Undo")
                {
                    PasswordTxtBx.Text = Password;
                    ClearPasswordBttn.Text = "Clear";
                }
            }
        }

        /// <summary>
        /// Defines the actions that are performed when clicking the Ok button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OkBttn_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(UserNameTxtBx.Text)) || (string.IsNullOrEmpty(PasswordTxtBx.Text)))
                MessageBox.Show("Please enter a UserName and a Password!");
            else
            {
                SmugMugLogIn();
                if (RemeberMeChckBx.Checked == true)
                {
                    Application.UserAppDataRegistry.SetValue("UserId", user.UserId);
                    Application.UserAppDataRegistry.SetValue("Password", user.PasswordHash);
                }
                else
                {
                    Application.UserAppDataRegistry.SetValue("UserId", string.Empty);
                    Application.UserAppDataRegistry.SetValue("Password", string.Empty);
                }
                this.DialogResult = DialogResult.OK;
            }
            UserNameTxtBx.Clear();
            PasswordTxtBx.Clear();
        }
        
        /// <summary>
        /// Defines the actions that are performed when clicking the Cancel button (close)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelBttn_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
